package com.quanquan.user.controller;

import DTO.PageDTO;
import base.BaseController;
import com.quanquan.user.service.autoCompute.dto.TableField;
import com.quanquan.user.service.autoCompute.entity.MemberLabel;
import com.quanquan.user.service.autoCompute.request.MemberLabelRequestBody;
import com.quanquan.user.service.autoCompute.service.MemberLabelService;
import enums.ResCodeEnum;
import exceptions.service.ServiceRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;


/**
 * @author gongzhou
 * @title: MemberLabelController
 * @projectName fuyang
 * @description: TODO
 * @date 2019/6/229:15
 */


@RestController
@RequestMapping("/memberLabel")
public class MemberLabelController extends BaseController {

    @Autowired
    private MemberLabelService memberLabelService;

    /**
     * 获取会员的所有字段
     *
     * @return
     */
    @GetMapping("/fields/{type}")
    public ResultBody getMemberFields(@PathVariable String type) {
        if (StringUtils.isEmpty(type)) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        List<TableField> fieldList = memberLabelService.getFields(type);
        if (fieldList.isEmpty() || fieldList == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), fieldList);
    }

    /**
     * 添加标签
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/create")
    public ResultBody create(@RequestBody MemberLabelRequestBody requestBody) {
        try {
            MemberLabel label = memberLabelService.create(requestBody);
            memberLabelService.syncLabel(label.getId());
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), label);
        } catch (ServiceRuntimeException e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 分页获取标签
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/list")
    public ResultBody list(@RequestBody MemberLabelRequestBody requestBody) {
        try {
            PageDTO<MemberLabel> result = memberLabelService.list(requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), result);
        } catch (Exception e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 根据Id获取标签
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ResultBody getOne(@PathVariable String id) {
        try {
            MemberLabel byId = memberLabelService.getById(id);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), byId);
        } catch (ServiceRuntimeException e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 查询所有标签的数量
     *
     * @param requestBodies
     * @return
     */
    @PostMapping("/countAll")
    public ResultBody countAll(@RequestBody List<MemberLabelRequestBody> requestBodies) {
        try {
            Long count = memberLabelService.countAll(requestBodies);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), count);
        } catch (ServiceRuntimeException e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 修改标签
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(@RequestBody MemberLabelRequestBody requestBody) {
        try {
            MemberLabel update = memberLabelService.update(requestBody);
            memberLabelService.syncLabel(update.getId());
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), update);
        } catch (ServiceRuntimeException e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @GetMapping("/remove/{id}")
    public ResultBody remove(@PathVariable String id) {
        try {
            memberLabelService.remove(id);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (ServiceRuntimeException e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), e.getMessage());
        }
    }

    /**
     * 手动同步标签分组
     *
     * @param id
     * @return
     */
    @GetMapping("/sync/{id}")
    public void syncLabelGroup(@PathVariable String id) {
        memberLabelService.syncLabel(id);
    }

}
